#include "Protocol.hpp"
#include "Sock.hpp"
#include <pthread.h>

void Usage(string proc)
{
    cout << "Usage \r\t" << proc << "port" << endl;
    exit(1);
}

void *HandlerRequest(void *args)
{
    int new_sock = *(int *)args;
    delete (int *)args;

    pthread_detach(pthread_self());

    // 1. 字节型传输
    // request_t req;
    // memset(&req, 0, sizeof(req));
    // ssize_t s = read(new_sock, &req, sizeof(req));
    // if (s == sizeof(req))
    // {
    //     response_t resp;
    //     memset(&resp, 0, sizeof(resp));
    //     int req_x = req._x;
    //     int req_y = req._y;
    //     char req_op = req._op;
    //     switch (req_op)
    //     {
    //     case '+':
    //         resp._result = req_x + req_y;
    //         break;
    //     case '-':
    //         resp._result = req_x - req_y;
    //         break;
    //     case '*':
    //         resp._result = req_x * req_y;
    //         break;
    //     case '/':
    //         if (req_y == 0)
    //             resp._code = -1;
    //         else
    //             resp._result = req_x / req_y;
    //         break;
    //     case '%':
    //         if (req_y == 0)
    //             resp._code = -2;
    //         else
    //             resp._result = req_x % req_y;
    //         break;
    //     default:
    //         resp._code = -3;
    //         break;
    //     }
    //     cout << "requeset: " << req._x << req._op << req._y << endl;
    //     write(new_sock, &resp, sizeof(resp));
    //     cout << "response finish!" << endl;
    // }
    // close(new_sock);

    // 2. 序列化、反序列化传输
    request_t req;
    memset(&req, 0, sizeof(req));
    response_t resp;
    memset(&resp, 0, sizeof(resp));
    char buffer[1024] = {0};
    ssize_t s = read(new_sock, buffer, sizeof(buffer));
    if (s > 0)
    {
        buffer[s] = 0;
        cout << "requese: " << buffer << endl;

        // 反序列化
        request_deserialization(buffer, req);
        switch (req._op)
        {
        case '+':
            resp._result = req._x + req._y;
            break;
        case '-':
            resp._result = req._x - req._y;
            break;
        case '*':
            resp._result = req._x * req._y;
            break;
        case '/':
            if (req._y == 0)
                resp._code = -1;
            else
                resp._result = req._x / req._y;
            break;
        case '%':
            if (req._y == 0)
                resp._code = -2;
            else
                resp._result = req._x % req._y;
            break;
        default:
            resp._code = -3;
            break;
        }

        cout << "requeset: " << req._x << req._op << req._y << endl;

        // 序列化
        string response_string = response_serialization(resp);
        write(new_sock, response_string.c_str(), response_string.size());

        cout << "response finish!" << response_string << endl;
    }

    close(new_sock);
}

int main(int argc, char *argv[])
{
    if (argc != 2)
        Usage(argv[0]);

    int sock = Sock::Socket();
    Sock::Bind(sock, atoi(argv[1]));
    Sock::Listen(sock);
    for (;;)
    {
        int new_sock = Sock::Accept(sock);
        cout << "A new client link..." << endl;

        int *pram = new int(new_sock);
        pthread_t tid;
        pthread_create(&tid, nullptr, HandlerRequest, pram);
    }

    return 0;
}
